package com.bike.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.SysMenu;
import com.bike.entity.SysRole;
import com.bike.mapper.SysMenuMapper;
import com.bike.mapper.SysRoleMapper;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.RightsHelper;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;

@Service
public class SysMenuServiceImpl extends BaseServcieImpl  {

	@Autowired
	SysMenuMapper sysMenuMapper;
	@Autowired
	SysRoleMapper sysRoleMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, SysMenu record) {
		if(record.getParentId()!=null&&record.getParentId()>0){
			record.setLevel(2);
		}else{
			record.setLevel(1);
		}
		if(pd.getLong("menuId")>0){
			 //编辑
			sysMenuMapper.updateByPrimaryKeySelective(record);
		}else{
			sysMenuMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteSysMenuById(long menuId) {
		int ressult=sysMenuMapper.deleteByPrimaryKey(menuId);
		if(ressult>0){
			sysMenuMapper.deleteByParentId(menuId);
		}
		return ressult;
	}
	
	/**
	*列表分页
	*/
	public PageData selectSysMenuList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<SysMenu> records= sysMenuMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<SysMenu>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<SysMenu> listAll(PageData pd) {
		return  sysMenuMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public SysMenu selectBySysMenuId(Long menuId) {
		return sysMenuMapper.selectByPrimaryKey(menuId);
	}

	public  List<SysMenu> selectBySysMenuTree(PageData pd) {
		long roleId=pd.getLong("roleId");
		if(roleId<=0){
			return new ArrayList<SysMenu>();
		}
		SysRole role=sysRoleMapper.selectByPrimaryKey(roleId);
		String roleRights=role.getMenuRights();
		if(StringUtils.isEmpty(roleRights)){
			roleRights="0";
		}
		 List<SysMenu> menuList=sysMenuMapper.selectAllOrderLeve(new PageData());
		 List<SysMenu> menuListNew=new ArrayList<>();
		 for(SysMenu menu:menuList){
			 menu.setTempRoleId(roleId);
			 List<SysMenu> childreList=menu.getChildren();
			 List<SysMenu> childreListNew=new ArrayList<>();
			 if(childreList!=null){
				 for(SysMenu menu2:childreList){
					 if( RightsHelper.testRights(roleRights, menu2.getMenuId().intValue())){
						 menu2.setChecked(true);
						 childreListNew.add(menu2);
						 if(pd.get("notAll")!=null){
							 menu.setChecked(true);
						 }
					 } else{
						 if(pd.get("notAll")==null&&role.getRoleCode().equals(Constant.adminRole)){
							 childreListNew.add(menu2);
						 }
					 }
					 menu2.setTempRoleId(roleId);
				 }
			 }
			menu.setChildren(childreListNew);
			if(menu.isChecked()||(pd.get("notAll")==null&&role.getRoleCode().equals(Constant.adminRole))){
				menuListNew.add(menu);
			}
		 }
		return menuListNew;
	}

	public List<SysMenu> selectBySysMenuTreeForRole(PageData pd,SysRole loginRole) {
		long roleId=pd.getLong("roleId");
		if(roleId<=0){
			return new ArrayList<SysMenu>();
		}
		SysRole role=sysRoleMapper.selectByPrimaryKey(roleId);
		String roleRights=role.getMenuRights();
		if(StringUtils.isEmpty(roleRights)){
			roleRights="0";
		}
		if(!loginRole.getRoleCode().equals(Constant.adminRole)){
			return new ArrayList<SysMenu>();
		}
		
		 List<SysMenu> menuList=sysMenuMapper.selectAllOrderLeve(new PageData());
		 
		 for(SysMenu menu:menuList){
			 menu.setTempRoleId(roleId);
			 List<SysMenu> childreList=menu.getChildren();
			 int flag=0;
			 if(childreList!=null){
				 for(SysMenu menu2:childreList){
					 if( RightsHelper.testRights(roleRights, menu2.getMenuId().intValue())){
						 menu2.setChecked(true);
					 } else{
						 flag=1;
					 }
					 menu2.setTempRoleId(roleId);
				 }
			 }
			 if(flag==1){
				 menu.setChecked(false);
			 }else{
				 menu.setChecked(true);
			 }
		 }
		return menuList;
	}
}

